package com.wlyuan.open.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lijing
 * @description
 */
public class IpUtils {

    static String[] IP_HEADS = new String[]{
            "x-forwarded-for",
            "Proxy-Client-IP",
            "WL-Proxy-Client-IP",
            "HTTP_CLIENT_IP",
            "HTTP_X_FORWARDED_FOR",
    };

    public static String getRemoteAddress(HttpServletRequest request) {
        for (String header : IP_HEADS) {
            String ip = request.getHeader(header);
            if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
                if (ip.contains(",")) {
                    ip = ip.substring(0, ip.indexOf(","));
                }
                return ip;
            }
        }
        return request.getRemoteAddr();
    }
}
